package com.zzw.basic.io;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;

public class Person implements Serializable{

	public Person(){

	}
	private static final long serialVersionUID = 1L;

	private String name;
	private int age;
	public Person(String name,int age){
		this.name = name;
		this.age = age;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getAge() {
		return age;
	}
	public void setAge(int age) {
		this.age = age;
	}
	
	private Object writeReplace() {
		ArrayList<Object> list = new ArrayList<>();
		list.add(name);
		list.add(age);
		return list;
	}

	public static void main(String[] args) {
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("test.txt"));
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));
			Person p = new Person("hello",5);
			oos.writeObject(p);
			ArrayList<Object> list = (ArrayList) ois.readObject();
			System.out.println(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}